import axios from "axios";
import qs from "qs";
import $data from "./globalData"
// import cryptoJs from "crypto-js";
// const cryptoJs = require("crypto-js");
// const qs = require("qs");

interface obj {
  [idx: string]: any
}
export interface response<T> {
  status: number;
  data: {
      data: T,
      errorcode: number,
      errormessage: string,
  }
}

// function genMac(data: Object):string{
  
//   let salt = $data.getSalt();
//   let orginString = JSON.stringify(data)
//   return cryptoJs.enc.Base64.stringify(cryptoJs.HmacSHA256(orginString,salt))
// }


//这是在reqest.js里
const instance = axios.create({
  // baseURL: 'http://127.0.0.1:50913',
  baseURL: 'http://ccras.sunshinebrid.world/api',
  timeout: 5000,
});

function login(url: string, data: obj) {
  // let mac=genMac(data)
  data.salt = $data.setSalt();
  return instance.post(url, qs.stringify(data, {arrayFormat: 'brackets'}), {
    "headers": {
      // "mac":mac
      // "Content-Type":"application/x-www-form-urlencode",
    },
  });
}


function get(url: string, data: Object) {
  if(data!=null&&JSON.stringify(data) != "{}"){
    url+='?'
  }
  return instance.get(url + qs.stringify(data, {arrayFormat: 'brackets'}), {
    "headers": {
      "token": $data.getUser().token,
      // "mac":genMac(data)
    }
  });
}



function post(url: string, data: Object) {
  return instance.post(url, qs.stringify(data), {
    "headers": {
      "token": $data.getUser().token,
      // "mac":genMac(data)
    }
  });
}

export default { login, get,post };
//这里是把所有方法一起暴露, 引入是整体, 用的时候记得拆开;
